﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Transactions;
using PagedList;

namespace DLL
{
    public class ReturnProductRepo
    {
        public bool AddReturnProduct(ReturnProduct returnProduct, List<ReturnProductDetail> details)
        {
            bool success = true;
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    try
                    {
                        // Add return product
                        dbContext.ReturnProduct.Add(returnProduct);
                        dbContext.SaveChanges();
                        if (returnProduct.Id > 0)
                        {
                            foreach (var returnDetail in details)
                            {
                                // Add each return product detail
                                ObjectParameter vbitSuccess = new ObjectParameter("vbitSuccess", typeof(bool));
                                dbContext.pAddReturnProductDetail(returnProduct.Id, returnDetail.ProductId, returnDetail.Amount, vbitSuccess);
                                success = Convert.ToBoolean(vbitSuccess.Value);
                                if (!success)
                                {
                                    break;
                                }
                            }
                            if (success)
                            {
                                transaction.Complete();
                            }
                        }
                        else
                        {
                            success = false;
                        }
                    }
                    catch
                    {
                        success = false;
                        throw;
                    }
                }
            }
            return success;
        }

        public bool AddReturnProductWithBill(ReturnProduct returnProduct, List<ReturnProductDetail> details)
        {
            bool success = true;
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    try
                    {
                        // Add return product
                        dbContext.ReturnProduct.Add(returnProduct);
                        dbContext.SaveChanges();
                        if (returnProduct.Id > 0)
                        {
                            foreach (var returnDetail in details)
                            {
                                // Add each return product detail
                                ObjectParameter vbitSuccess = new ObjectParameter("vbitSuccess", typeof(bool));
                                dbContext.pAddReturnProductDetailWithBill(returnProduct.Id, returnDetail.BillDetailId, returnDetail.Amount, vbitSuccess);
                                success = Convert.ToBoolean(vbitSuccess.Value);
                                if (!success)
                                {
                                    break;
                                }
                            }
                            if (success)
                            {
                                transaction.Complete();
                            }
                        }
                        else
                        {
                            success = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        // TODO: Log error
                        success = false;
                        throw ex;
                    }
                }
            }
            return success;
        }

        public List<pSearchReturnProduct_Result> Search(DateTime fromDate, DateTime toDate)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.pSearchReturnProduct(fromDate, toDate).ToList();
            }
        }

        public List<pGetReturnProductDetailByReturnProductId_Result> GetDetailById(int returnProductId)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                return dbContext.pGetReturnProductDetailByReturnProductId(returnProductId).ToList();
            }
        }

        public pGetTotalReturnPrice_Result GetTotalReturn(DateTime fromDate, DateTime toDate)
        {
            using (RetailManagementEntities dbContext = new RetailManagementEntities())
            {
                var result = dbContext.pGetTotalReturnPrice(fromDate, toDate).FirstOrDefault();
                return result;
            }
        }
    }
}
